<?php

class OrderService extends AbstractService
{
    /**
     * @var OrderService
     */
    private static $instance;

    /**
     * Get the instance of OrderService.
     *
     * @return OrderService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new OrderService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    protected function __construct()
    {
        parent::__construct();
    }

    /**
     * 处理创建时的逻辑
     * 1. 游戏初始化时获取第一块订单板的初始槽位的订单信息， 后续的订单板需要前端提交接口进行订单初始化
     *
     * @param int $userId
     * @return void
     */
    public function handleCreate($userId)
    {

    }

    /**
     * 处理登录时的逻辑
     * 1. 初始化火车新订单
     * 2. 清空建筑订单完成数量
     *
     * @param int $userId
     * @return void
     */
    public function handleLogin($userId)
    {
        /**
         * @var $profileInfoList ModuleProfileInfoVoList
         * @var $orderCountInfo ModuleProfileInfoVo
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $orderCountInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_ORDER_COMPLETE_COUNT, false);
        if (!$orderCountInfo) {
            return;
        }
        $lastOrderTime = $orderCountInfo->getTime();
        if (GameService::get()->isDailyFirstLogin($userId) == false || SzTime::getDate($lastOrderTime) == SzTime::getDate(GameService::get()->getReqTime())) {
            return;
        }
        $this->clearOrderCount($userId, $profileInfoList);
        $this->clearOrderMissions($userId, $profileInfoList);
    }

    /**
     * 提供当前 service 数据
     *
     * @param int $userId
     * @param boolean $visitMode default false
     * @return array(OrderVoList, OrderBoardVoList)
     */
    public function info($userId, $visitMode = false)
    {
        $orderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ORDER);
        $orderBoardList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ORDER_BOARD);
        $orderBoardRewardList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ORDER_BOARD_REWARD);

        return array(
            $orderList,
            $orderBoardList,
            $orderBoardRewardList
        );
    }

    /**
     * 解锁槽位时刷新新订单
     * @param $userId
     * @param $position
     * @param null $orderList
     * @throws SzException
     * @return OrderVo
     */
    public function flushOrder($userId, $position, $orderList = null)
    {
        /**
         * @var OrderVoList $orderList
         * @var OrderVo $order
         */
        if (!$orderList) {
            $orderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ORDER);
        }
        if (!in_array($position, json_decode(GameConst::ORDER_TYPE_NORMAL))) {
            throw new SzException(20354, $position);
        }
        $order = $orderList->getElement($position, false);
        if ($order) {
            throw new SzException(20353, $position);
        }
        $unlockFlowers = SeedService::get()->getUnlockFlowerList($userId);
        if (count($unlockFlowers) <= count($orderList->getList())) {
            throw new SzException(20369, $position);
        }
        $newOrderVo = $this->getNewOrder($userId, $position, null, $orderList, null,true);
        $orderList->addElement($newOrderVo);
        $orderList->setElement($position, $newOrderVo);
        SzPersister::get()->setVoList($orderList);
        return $newOrderVo;
    }

    /**
     * 获得新订单
     * @param int $userId
     * @param int $position
     * @param OrderVo $oldOrder
     * @param OrderVoList $orderList
     * @param array $orderSettings
     * @throws SzException
     * @return OrderVo
     */
    private function getNewOrder($userId, $position, $oldOrder = null, $orderList = null, $orderSettings = null, $isCreate = false)
    {
        $completeOrderCount = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_ORDER_COMPLETE_COUNT);
        $videoOrderSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_VIDEO_ORDER, 1, true);
        $level = UserService::get()->getUserLevel($userId);
        if ($level >= $videoOrderSetting['level'] && in_array($completeOrderCount,$videoOrderSetting['amountCompleted']) && !$isCreate) {
            // 视频订单id
            $newOrderId = 99999999;
            $completeCd = $videoOrderSetting['completeCd'];
            $targets = [];
            list($min,$max) = $videoOrderSetting['randomQuantity'];
            $award_num = mt_rand($min,$max);
            $reward = array($videoOrderSetting['rewardProps']=>$award_num);
        } else {
            $newOrderId = 88888888;
            $userLevel = UserService::get()->getUserLevel($userId);
            $orderSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_ORDER2, $userLevel, true);
            if (!$orderSetting) {
                throw new SzException(80001,GameConst::CONFIG_ORDER2.'-'.$userLevel);
            }
            $unlockFlowers = SeedService::get()->getUnlockFlowerList($userId);
            if (count($unlockFlowers) == 1) {
                $randList = array(
                    1 => $orderSetting['probability1'],
                );
            } else if (count($unlockFlowers) == 2) {
                $randList = array(
                    1 => $orderSetting['probability1'],
                    2 => $orderSetting['probability2']
                );
            } else {
                $randList = array(
                    1 => $orderSetting['probability1'],
                    2 => $orderSetting['probability2'],
                    3 => $orderSetting['probability3']
                );
            }
            $num = SzUtility::getRandomElementByProbability($randList);
            $total = $orderSetting['quantity'];
            if ($total < $num) {
                throw new SzException(80001,GameConst::CONFIG_ORDER2.'-'.$userLevel);
            }
            $reward = $targets = array();
            $completeCd = $orderSetting['time'];
            for ( $i=1; $i<=$num; $i++) {
                $key = array_rand($unlockFlowers);
                $flowerId = $unlockFlowers[$key];
                unset($unlockFlowers[$key]);
                $orderRewardSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_ORDER_REWARD, $flowerId, true);
                if (!$orderRewardSetting) {
                    throw new SzException(80001,GameConst::CONFIG_ORDER_REWARD.'-'.$flowerId);
                }
                if ($num == $i) {
                    $count = $total;
                } else {
                    $count = mt_rand(1,($total - ($num - $i)));
                }
                $total -= $count;
                $buildEntityId = EntityService::get()->buildEntityId(GameConst::ENTITY_ACTION_CONSUME,GameConst::ENTITY_TYPE_ITEM,$flowerId);
                $targets[$buildEntityId] = $count;
            }

            foreach ($targets as $flower=>$flowerNum){
                if (!isset($reward[GameConst::ENTITY_ACTION_GET_TOKEN])) {
                    $reward[GameConst::ENTITY_ACTION_GET_TOKEN] = 0;
                }

                if (!isset($reward[GameConst::ENTITY_ACTION_GET_EXP])) {
                    $reward[GameConst::ENTITY_ACTION_GET_EXP] = 0;
                }

                $fid = substr($flower,-8);
                //20200409
                //获取玩家该花朵是否具有金币数量加成
                $flowerReward = SzConfig::get()->loadAppConfig(GameConst::CONFIG_ORDER_REWARD, $fid, true);
                $goldNum = $flowerReward['gold']*$flowerNum;
                $experienceNum = $flowerReward['experience']*$flowerNum;
                $cashBuff = FlowersUpgradeService::get()->getFlowerBuff($userId,$fid,GameConst::FLOWER_TYPE_1);
                if (!empty($cashBuff)){
                    if ($cashBuff[0] == 2){
                        $goldNum = ($flowerReward['gold'] + $cashBuff[1])*$flowerNum;
                    }
                    if ($cashBuff[0] == 1){
                        $goldNum = ceil($flowerReward['gold']*(1+$cashBuff[1]/100))*$flowerNum;
                    }
                }
                //20200409
                //获取玩家该花朵是否具有经验加成
                $experienceBuff = FlowersUpgradeService::get()->getFlowerBuff($userId,$fid,GameConst::FLOWER_TYPE_2);
                if (!empty($experienceBuff)){
                    if ($experienceBuff[0] == 2){
                        $experienceNum = ($flowerReward['experience'] + $experienceBuff[1])*$flowerNum;
                    }
                    if ($experienceBuff[0] == 1){
                        $experienceNum = ceil($flowerReward['experience']*(1+$experienceBuff[1]/100))*$flowerNum;
                    }
                }
                $reward[GameConst::ENTITY_ACTION_GET_EXP] += intval($experienceNum);
                $reward[GameConst::ENTITY_ACTION_GET_TOKEN] += intval($goldNum);
            }
        }

        if (!$oldOrder) {
            $newOrder = new OrderVo($userId, $position, $newOrderId, json_encode($targets), json_encode($reward), json_encode([]), $completeCd, 0,true);
            return $newOrder;
        } else {
            $oldOrder->setPosition($position);
            $oldOrder->setOrderId($newOrderId);
            $oldOrder->setTarget($targets);
            $oldOrder->setReward($reward);
            $oldOrder->setExtraDrop([]);
            $oldOrder->setStatus(0);
            $oldOrder->setCooldown(GameService::get()->getReqTime() + $completeCd);
            return $oldOrder;
        }
    }

    /**
     * 获取订单需求物品
     * @param $orderSetting
     * @return array
     */
    private function getTargetIds($orderSetting)
    {
        $targetIdArr = [];
        for ($i = 1; $i <= 3; $i++) {
            if ($orderSetting["targetId_$i"] == -1) {
                continue;
            }
            list($action, $module, $subId) = EntityService::get()->parseEntityId($orderSetting["targetId_$i"]);
            $targetIdArr[] = $subId;
        }
        return $targetIdArr;
    }

    /**
     * 判断订单需求物品是否都已解锁
     * @param $unlockFlowers
     * @param $orderSetting
     * @return bool
     */
    private function isFlowersUnlock($unlockFlowers, $orderSetting)
    {
        $targetIdArr = $this->getTargetIds($orderSetting);
        foreach ($targetIdArr as $targetId) {
            if (in_array($targetId, $unlockFlowers)) {
                continue;
            }
            return false;
        }
        return true;
    }

    /**
     * 完成订单（除火车订单以外的订单）
     * @param $userId
     * @param $position
     * @param $orderId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function complete($userId, $position, $orderId, $reqTime)
    {
        $res = array(
            'code' => 0,
            'order' => array()
        );
        /**
         * @var OrderVoList $orderList
         * @var OrderVo $order
         */
        $orderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ORDER);
        $order = $orderList->getElement($position);
        // 检查订单ID是否正确
        if ($orderId != $order->getOrderId()) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ORDER_ID_ERR'];//订单id错误
            return $res;
//            throw new SzException(20351, array($orderId, $order->getOrderId()));
        }

        // 检查是否到达订单可提交时间
        if ($reqTime < $order->getCooldown()) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ORDER_COOL_DOWN_NOT_FINISH'];//订单冷却时间未到
            return $res;
//            throw new SzException(20352);
        }

        // 扣除订单所需要的物品
        $inputTid = '';
        // 视频订单id
        if ($order->getOrderId() == 99999999) {
            if (!$order->getStatus()) {
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ORDER_VIDEO_NOT_FINISH'];//视频订单未完成
                return $res;
//                throw new SzException(20373);
            }
        } else if ($order->getOrderId() != 77777777) {
            foreach ($order->getRawTarget() as $_entityId => $_count) {
                GameService::get()->handleEntity($userId, $_entityId, $_count, true, true);
                $inputTid .= $_entityId . ':' . abs($_count) . '|';
            }
        }

        $addition = 1;
        $orderBuffStatus = VideoService::get()->getBuffStatus($userId,GameConst::VIDEO_BUFF_ORDER,$reqTime);
        if ($orderBuffStatus) {
            $videoSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_VIDEO, GameConst::VIDEO_BUFF_ORDER, true);
            if ($videoSetting) {
                $addition = $videoSetting['sp_reward'][0];
            }
        }

//        // 保存完成订单的奖励
//        $this->createBoardOrderReward($userId, $order, false);
        // 获得订单奖励
        $outputTid = '';
        foreach ($order->getRawReward() as $_entityId => $_count) {
            if ($orderBuffStatus && $_entityId == GameConst::ENTITY_ACTION_GET_TOKEN) {
                $_count = $_count * $addition;
            }
            $outputTid .= $_entityId . ':' . $_count . '|';
            GameService::get()->handleEntity($userId, $_entityId, $_count, true);
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_NORMAL_ORDER,$userId,1,$outputTid,'complete');//获得资源上报

        // 获得订单额外掉落
        foreach ($order->getRawExtraDrop() as $_entityId => $_count) {
            GameService::get()->handleEntity($userId, $_entityId, $_count, true, true);
        }

        $completeOrderCount = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_ORDER_COMPLETE_COUNT);
        UserService::get()->updateProfileInfo($userId, GameConst::USER_INFO_TYPE_ORDER_COMPLETE_COUNT, $completeOrderCount + 1);

        // 刷新新的订单
        $orderList->updateElement($position, $this->getNewOrder($userId, $position, $order, $orderList));
        SzPersister::get()->setVoList($orderList);
        // 更新任务进度
        TreeService::get()->updateProgress($userId,GameConst::TREE_TASK_COMPLETE_ORDER,1);

        NewTaskService::get()->updateDailyTask($userId,GameConst::DAILY_TASK_ORDER,1);

        // 更新活动任务进度
        $activityData = ActivityService::get()->getActivityIdAndPosition($userId,GameConst::ACTIVITY_TASK_3);
        if (!empty($activityData)){
            ActivityTaskService::get()->updateActivityTaskProgress($userId,$activityData['aid'],$activityData['position']);
        }

        // 更新合约任务进度
        $contractData = ContractService::get()->getContractOpenAndTask($userId,GameConst::CONTRACT_TASK_1,$reqTime);
        if (!empty($contractData) && !empty($contractData['task']) && !empty($contractData['session'])){
            if ($reqTime >= $contractData['start'] && $reqTime <= $contractData['end']){
                ContractService::get()->updateTaskProgress($userId,$contractData['session'],$contractData['task']['pos'],$contractData['task']['taskId'],$reqTime);
            }
        }

        //社团竞赛任务进度添加
        UnionCompetitionService::get()->addCompetitionTaskProcess($userId,GameConst::UNION_COMPETITION_TASK_11, $reqTime,1);

        //跟新店铺经营数据
        UserShopService::get()->record($userId,1,2003,1,$reqTime);

        // 数据采集：订单提交
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:order|action:start|',
                'item' => $orderId,
                'value' => 1
            )
        ));

        /**
         * @var ModuleProfileInfoVo $profileInfo
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_ORDER_COUNT, false);
        if ($profileInfo) {
            $num = $profileInfo->getRawInfo();
            $num++;
            $profileInfo->setTime($reqTime);
            $profileInfo->setInfo(intval($num));
            $profileInfoList->updateElement(GameConst::USER_INFO_TYPE_ORDER_COUNT, $profileInfo);
            SzPersister::get()->setVoList($profileInfoList);
        }

        $res['order'] = $order->toArray();
        return $res;
    }

    /**
     * 领取奖励
     *
     * @param $userId
     * @param $position
     * @return OrderBoardRewardVo
     */
    public function receiveBoardOrderReward($userId, $position)
    {
        /**
         * @var OrderBoardRewardVoList $orderBoardRewardList
         * @var OrderBoardRewardVo $orderBoardReward
         */
        $orderBoardRewardList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ORDER_BOARD_REWARD);
        $orderBoardReward = $orderBoardRewardList->getElement($position);

        // 获得订单奖励
        $outputTid = '';
        foreach ($orderBoardReward->getRawReward() as $_entityId => $_count) {
            $outputTid .= $_entityId . ':' . $_count . '|';
            GameService::get()->handleEntity($userId, $_entityId, $_count, true);
        }

        // 获得订单额外掉落
        foreach ($orderBoardReward->getRawExtraDrop() as $_entityId => $_count) {
            GameService::get()->handleEntity($userId, $_entityId, $_count, true, true);
        }

        // 删除奖励内容
        $orderBoardRewardList->deleteElement($position, $orderBoardReward);
        SzPersister::get()->setVoList($orderBoardRewardList);

        return $orderBoardReward;
    }

    /**
     * 保存领奖信息
     *
     * @param $userId
     * @param $order OrderVo
     * @param $isBonus bool
     * @return void
     */
    public function createBoardOrderReward($userId, $order, $isBonus)
    {
        $position = $order->getPosition();

        /**
         * @var OrderBoardRewardVoList $orderBoardRewardList
         * @var OrderBoardRewardVo $orderBoardReward
         */
        $orderBoardRewardList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ORDER_BOARD_REWARD);
        $orderBoardReward = $orderBoardRewardList->getElement($position, false);

        if (!$orderBoardReward) {
            $orderBoardReward = new OrderBoardRewardVo($userId, 0, 0, json_encode([]), json_encode([]), 0, true);
        }

        // 更新奖励信息
        $orderBoardReward->setPosition($order->getPosition());
        $orderBoardReward->setOrderId($order->getOrderId());
        $orderBoardReward->setReward($order->getRawReward());
        $orderBoardReward->setExtraDrop($order->getRawExtraDrop());
        $orderBoardReward->setIsBonus($isBonus ? 1 : 0);

        if ($orderBoardReward->isInsert() && !SzUtility::checkArrayKey($position, $orderBoardRewardList->getList())) {
            $orderBoardRewardList->addElement($orderBoardReward);
            $orderBoardRewardList->setElement($position, $orderBoardReward);
        } else {
            $orderBoardRewardList->updateElement($position, $orderBoardReward);
        }

        SzPersister::get()->setVoList($orderBoardRewardList);
    }

    /**
     * 完成首个订单并看到感谢信
     *
     * @param int $userId
     * @return boolean
     */
    public function doneFirstOrder($userId)
    {
        UserService::get()->updateProfileInfo($userId, GameConst::USER_INFO_TYPE_IS_DONE_FIRST_ORDER, 1);
        return true;
    }

    /**
     * 领取完成订单数量奖励
     * @param $userId
     * @param $purposeId
     * @throws SzException
     * @return  array
     */
    public function getOrderDailyMissionReward($userId, $purposeId)
    {
        $dailyPurposeConfig = SzConfig::get()->loadAppConfig(GameConst::CONFIG_DAILY_PURPOSE, $purposeId, true);
        $completeOrderCount = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_ORDER_COMPLETE_COUNT);
        $completeOrderMissions = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_ORDER_COMPLETE_MISSIONS);
        if ($completeOrderMissions && in_array($dailyPurposeConfig['treasureChest'], $completeOrderMissions)) {
            throw new SzException(20371, $purposeId);
        }
        if ($dailyPurposeConfig['leve'] > UserService::get()->getUserLevel($userId)) {
            throw new SzException(20371, $purposeId);
        }
        if ($dailyPurposeConfig['goals'] > $completeOrderCount) {
            throw new SzException(20370, [$purposeId, $completeOrderCount]);
        }
        $reward = $dailyPurposeConfig['reward1'];
        $outputTid = '';
        $result = [];
        foreach ($reward as $entityId => $count) {
            list($tmpRandReward) = GameService::get()->handleEntity($userId, $entityId, $count, true, true);
            $result[] = $tmpRandReward;
            foreach ($tmpRandReward as $k1=>$v1) {
                $outputTid .= $k1 . ':' . abs($v1) . '|';
            }
        }
        $completeOrderMissions[] = $dailyPurposeConfig['treasureChest'];
        UserService::get()->updateProfileInfo($userId, GameConst::USER_INFO_TYPE_ORDER_COMPLETE_MISSIONS, $completeOrderMissions);

        // 数据采集:订单阶段奖励领取宝箱
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:order|action:achieve|',
                'item' => $purposeId
            )
        ));

        // 数据采集:订单阶段奖励领取宝箱产出
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_OUTPUT, array(
            'clientData' => array(
                'tid' => $outputTid,
                'item' => 'order_achieve&' . $purposeId
            )
        ));
        return $result;
    }

    /**
     * @param $userId
     * @param $profileInfoList ModuleProfileInfoVoList
     */
    public function clearOrderCount($userId, $profileInfoList)
    {
        UserService::get()->updateProfileInfo($userId, GameConst::USER_INFO_TYPE_ORDER_COMPLETE_COUNT, 0, $profileInfoList);
    }

    /**
     * @param $userId
     * @param $profileInfoList ModuleProfileInfoVoList
     */
    public function clearOrderMissions($userId, $profileInfoList)
    {
        UserService::get()->updateProfileInfo($userId, GameConst::USER_INFO_TYPE_ORDER_COMPLETE_MISSIONS, [], $profileInfoList);
    }

    /**
     * @param $userId
     * @throws SzException
     */
    public function clearVideoOrder($userId) {
        $orderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ORDER);
        if (!$orderList) {
            return;
        }

        $reqTime = GameService::getReqTime();

        /**
         * @var ModuleProfileInfoVo $profileInfo
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_CLEAR_VIDEO_ORDER, false);
        if (!$profileInfo) {
            $profileInfo = new ModuleProfileInfoVo(
                $userId,
                GameConst::USER_INFO_TYPE_CLEAR_VIDEO_ORDER,
                1,
                $reqTime,
                true
            );
            $profileInfoList->addElement($profileInfo);
            $profileInfoList->setElement(GameConst::USER_INFO_TYPE_CLEAR_VIDEO_ORDER, $profileInfo);
            SzPersister::get()->setVoList($profileInfoList);
        } else {
            // 日期不一致
            if(SzTime::getDate($profileInfo->getTime()) != SzTime::getDate($reqTime)) {
                $profileInfo->setTime($reqTime);
                $profileInfo->setInfo(1);
                $profileInfoList->updateElement(GameConst::USER_INFO_TYPE_CLEAR_VIDEO_ORDER, $profileInfo);
                SzPersister::get()->setVoList($profileInfoList);
            } else {
                if (SzTime::getDate($reqTime) != '2018-08-06') {
                    return;
                }
            }
        }
        /**
         * @var OrderVoList $orderList
         * @var OrderVo $order
         */
        foreach ($orderList->getList() as $position=>$orders) {
            $order = $orderList->getElement($position, false);
            if ($order->getOrderId() == 99999999 && !$order->getStatus()) {
                $cooldown = $order->getCooldown();
                if(SzTime::getDate($cooldown) != SzTime::getDate($reqTime)) {
                    // 刷新新的订单
                    $orderList->updateElement($position, $this->getNewOrder($userId, $position, $order, $orderList,null,true));
                }
            }
        }
        SzPersister::get()->setVoList($orderList);
    }

    /**
     * 每日刷新
     * @param $userId
     * @throws SzException
     */
    public function updateFirstPositionOrder($userId) {
        /**
         * @var VideoVo $video
         * @var OrderVoList $orderList
         * @var OrderVo $order
         */
        $orderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ORDER);
        if (!$orderList) {
            return array();
        }

        $order = $orderList->getElement(1,false);
        if ($order && $order->getOrderId() == 77777777) {
            // 刷新新的订单
            $orderList->updateElement(1, $this->getNewOrder($userId, 1, $order, $orderList));
            SzPersister::get()->setVoList($orderList);
        }

        return array();
//        $videoSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_VIDEO, GameConst::VIDEO_BUFF_ORDER, true);
//        $userLevel = UserService::get()->getUserLevel($userId);
//        if (!$videoSetting || $videoSetting['sp_lv'] > $userLevel) {
//            return array();
//        }
//
//
//        $reqTime = GameService::getReqTime();
//        $videoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_VIDEO);
//        $video = $videoList->getElement(GameConst::VIDEO_BUFF_ORDER, false);
//
//        if ($video && (SzTime::getDate($video->getLastClaimTime()) == SzTime::getDate($reqTime))) {
//            return array();
//        }
//
//        $orderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ORDER);
//        if (!$orderList) {
//            return array();
//        }
//
//        $order = $orderList->getElement(1,false);
//        if (!$order || $order->getOrderId() == 77777777) {
//            return array();
//        }
//
//        $order->setPosition(1);
//        $order->setOrderId(77777777);
//        $order->setTarget([]);
//        $order->setReward([]);
//        $order->setExtraDrop([]);
//        $order->setStatus(0);
//        $order->setCooldown($reqTime);
//        $orderList->updateElement(1,$order);
//        SzPersister::get()->setVoList($orderList);
//        return $order->toArray();
    }
}
